package com.investmenttechnologyservices.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Vector;

import org.mortbay.log.Log;

public class YahooRealTimeQuoteLoader {
	
	public static String makeQuoteURLString(List<String> symbols) {
		String symbolsString = "";
		Integer i = 0;
		for (String symbol : symbols) {
			symbolsString += ((i != 0) ? "," : "") + symbol.toUpperCase();
			i++;
		}
		final String qs = "http://finance.yahoo.com/download/javasoft.beans?SYMBOLS=" +
			symbolsString +"&format=sl";
		System.out.println("qs:"+qs);
		return qs;
	}
	
	/**
	* Example:
	* <p/>
	* <code>
	* String s[] = 	UrlUtils.getUrlString("http://www.docjava.com");
	* </code>
	*
	* @param urlString input urlString
	* @return array showing contents of web page.
	*/
	public static String[] getUrlString(String urlString) {
		Vector<String> v = getUrlVector(urlString);
		String s[] = new String[v.size()];
		v.copyInto(s);
		return s;
	}
	
	public static Vector<String> getUrlVector(String urlString) {
		Log.info("[getUrlVector(in): urlString="+urlString+"]");
		Vector<String> v = new Vector<String>();
		BufferedReader br = null;
		try {
			URL url = new URL(urlString);
			br = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			while (null != (line = br.readLine()))
				v.addElement(line);
			Log.info("[getUrlVector(out): # lines="+v.size()+", \nv="+v+"]");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return v;
	}
}
